import type { ArrangeTemplate } from './arrangeTemplate'
import type { ClassDetailResp, ClassInfo } from './classInfo'
import type { Lesson, LessonStudent } from './lesson'
import type { StudentResp, StudentStatResp } from './student'
import type { TimeTemplate } from './timeTemplate'
import http from '@/utils/http'

const BASE_URL = '/education/enrollment'

export interface Enrollment {
  id: string
  studentId: string
  classId: string
  enrollLessonCount: number
  completedLessonCount: number
  cancelLessonCount: number
  remainClassChangeCount: number
  remainLessonChangeCount: number
  renewalClassIds?: string[]
  status: string
  sellerId: string
  remark?: string
  isRenewal: boolean
}

export interface EnrollmentResp extends Enrollment {
  student: StudentStatResp
  classInfo: ClassInfo
  sellerName?: string
  leadTeacherId: string
  leadTeacherName: string
  campusId: string
  campusName: string
}

export interface EnrollmentQuery {
  studentId?: string
  classId?: string
  leadTeacherId?: string
  sellerId?: string
  status?: string[]
  createDateFrom?: string
  createDateTo?: string
}

export interface EnrollClassResp extends ClassInfo {
  enrollment: Enrollment
  arrangeTemplate: ArrangeTemplate
  timeTemplate: TimeTemplate
  leadTeacherName: string
  campusName: string
}

export interface EnrollLessonResp extends Lesson {
  orderId: string
  lessonStudent: LessonStudent
  leadTeacherName: string
  campusName: string
  classroomName: string
}

export interface EnrollmentDetailResp extends Enrollment {
  student: StudentResp
  classInfo: ClassDetailResp
  sellerName: string
  enrollLessonList: EnrollLessonResp[]
}

export interface EnrollmentChangeInfoResp {
  id: string
  createUser: string
  createTime: string
  updateUser: string
  updateTime: string
  enrollmentId: string
  changeType: string
  originalClassId: string
  originalLessonIndexes: string
  targetClassId: string
  targetLessonIndexes: string
  details: string
  businessId: string
  operateSource: string
  operateStatus: string
}

export interface EnrollPermissionImportReq {
  importKey: string
}

export interface EnrollPermissionImportResp {
  importKey: string
  totalRows: number
  validRows: number
}

export interface EnrollClassQuery {
  studentId?: string
  enrollStatus?: string
  classStatus?: number[]
}

export interface EnrollLessonQuery {
  studentId: string
  enrollmentId: string
}

export const getEnrollmentPage = (query: EnrollmentQuery & PageQuery) => {
  return http.get<Page<EnrollmentResp[]>>(`${BASE_URL}/page`, query)
}

export const exportEnrollmentList = (query: EnrollmentQuery) => {
  return http.download(`${BASE_URL}/export`, query)
}

export const getEnrollmentDetail = (id: string) => {
  return http.get<EnrollmentDetailResp>(`${BASE_URL}/detail/${id}`)
}

export const getChangeInfo = (id: string) => {
  return http.get<EnrollmentChangeInfoResp[]>(`${BASE_URL}/change/query/${id}`)
}

export const addEnroll = (data: { enrollmentId: string, sellerId: string }) => {
  return http.post(`${BASE_URL}/addSeller`, data)
}

export function getEnrollmentId(query: { studentId: string, classId: string }) {
  return http.get(`${BASE_URL}/getEnrollmentId`, query)
}

// 下载模板
export function downloadEnrollPermissionUserTemplate() {
  return http.download(`${BASE_URL}/downloadEnrollPermissionUserTemplate`)
}

/** 文件上传 */
export function upload(query: EnrollPermissionImportReq) {
  return http.post(`${BASE_URL}/import`, query)
}

// 解析导入数据
export function parseImportEnrollPermission(formData: any) {
  return http.post(`${BASE_URL}/parseImportEnrollPermission`, formData)
}

/** 查询学生报名班级分页 */
export function getEnrollClassPage(query: EnrollClassQuery & PageQuery) {
  return http.get<Page<EnrollClassResp[]>>(`${BASE_URL}/enroll-class/page`, query)
}

/** 查询学生报名班级 */
export function getEnrollClass(enrollmentId: string) {
  return http.get<EnrollClassResp>(`${BASE_URL}/enroll-class/detail/${enrollmentId}`)
}

/** 查询学生报名课次列表 */
export function getEnrollLessonList(query: EnrollLessonQuery) {
  return http.get<EnrollLessonResp[]>(`${BASE_URL}/enroll-lesson-list`, query)
}
